package com.clwl.supervise.superviseapp.service.pro;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clwl.supervise.superviseapp.entity.pro.*;
import com.clwl.supervise.superviseapp.mapper.pro.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@Service
public class JgProStaticScoreService extends ServiceImpl<JgProStaticScoreMapper, JgProStaticScore> {

    @Resource
    private JgFoodAdditiveTypeDetailMainMapper jgFoodAdditiveTypeDetailMainMapper;

    @Resource
    private JgTcCompanyMapper jgTcCompanyMapper;

    @Resource
    private JgProStaticScoreFoodAdditiveTypeMapper jgProStaticScoreFoodAdditiveTypeMapper;


    @Resource
    private JgProStaticScoreRiskContentMapper jgProStaticScoreRiskContentMapper;


    @Resource
    private JgProStaticRiskContentMainMapper jgProStaticRiskContentMainMapper;





    @Transactional
    public boolean staticScore(JgProStaticScore jgProStaticScore){
        if(this.baseMapper.insert(jgProStaticScore)>0){
            JgTcCompany tcCompany = jgTcCompanyMapper.selectById(jgProStaticScore.getCompanyId());
            //保存评分信息数据（食品类别 企业规模 食品生产许可现场核查结果）
            List<JgProFoodAdditiveTypeDetailMain> foodAdditiveTypeDetailMainList = jgFoodAdditiveTypeDetailMainMapper.selectFoodAdditiveTypeByTypeCode(Arrays.asList(tcCompany.getSplbbh().split(",")));
            for(JgProFoodAdditiveTypeDetailMain foodAdditiveTypeDetailMain : foodAdditiveTypeDetailMainList){
                JgProStaticScoreFoodAdditiveType foodAdditiveType = new JgProStaticScoreFoodAdditiveType();
                BeanUtil.copyProperties(foodAdditiveTypeDetailMain,foodAdditiveType,"id");
                foodAdditiveType.setStaticScoreId(jgProStaticScore.getId());
                jgProStaticScoreFoodAdditiveTypeMapper.insert(foodAdditiveType);
            }
            List<JgProStaticRiskContentMain> riskContentList = jgProStaticRiskContentMainMapper.selectList(new QueryWrapper<>());
            for(JgProStaticRiskContentMain riskContentMain : riskContentList){
                JgProStaticScoreRiskContent staticScoreRiskContent = new JgProStaticScoreRiskContent();
                BeanUtil.copyProperties(riskContentMain,staticScoreRiskContent,"id");
                staticScoreRiskContent.setStaticScoreId(jgProStaticScore.getId());
                jgProStaticScoreRiskContentMapper.insert(staticScoreRiskContent);
            }

            return true;
        } else {
            return false;
        }
    }
}
